﻿
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Corina.DataContext.Service
{
    public class ManagerService : AccountVerifyService
    {
        public ResultValue Login(string connectionString, string account, string password)
        {
            return VerifyAccount<ManagerInfo>(connectionString, account, password);
        }

        public bool Update(string connectionString, ManagerInfo entity, EntityOperation operation)
        {
            return DbOperator.UpdateEntity(connectionString, entity, operation);
        }

        public bool UpdateAvatar(string connectionString, int accountId, string fileName)
        {
            return UpdateAvatar(connectionString, accountId, fileName, AccountTypeValue.Manager);
        }

        public bool UpdatePassword(string connectionString, string account, string password)
        {
            string queryString = "UPDATE t_manager SET [Password] =@Password WHERE Account=@Account";
            List<SqlParameter> paras = new List<SqlParameter>();
            paras.Add(new SqlParameter("@Password", password));
            paras.Add(new SqlParameter("@Account", account));

            return DbOperator.UpdateEntity(connectionString, queryString, paras.ToArray());
        }

        public bool UpdateStatus(string connectionString, int accountId, AccountStatusValue status)
        {
            return UpdateStatus<ManagerInfo>(connectionString, accountId, status);
        }

        public bool UpdateRole(string connectionString, int accountId, int roleId)
        {
            return DbOperator.UpdateEntityById<ManagerInfo>(connectionString, accountId, new List<string>() { "RoleId" }, new List<object>() { roleId });
        }

        public DataTable GetAllItems(string connectionString)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT tm.*, tr.Name AS Role FROM t_manager AS tm ");
            builder.Append("LEFT JOIN CORINA_BASE.dbo.t_role AS tr ON tr.Id = tm.RoleId ");

            return DbOperator.GetDataTable(connectionString, builder.ToString());
        }
    }
}
